package Thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

public class Demo30 {
    public static void main(String[] args) {
        // 生产者-消费者模式
        BlockingDeque<Integer> queue = new LinkedBlockingDeque<>(1000);
        Thread producer = new Thread(() -> {
            int n=0;
            while(true) {
                try {
                    System.out.println("Producer " + n);
                    queue.put(n++);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "Producer");
        Thread consumer = new Thread(() -> {
            while(true) {
                try {
                    System.out.println("Consumer " + queue.take());

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"Consumer");
        producer.start();
        consumer.start();
    }
}
